package com.monicash.monicash.information.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.monicash.monicash.R;
import com.monicash.monicash.base.BaseFragment;
import com.monicash.monicash.bean.AppKycPersonalInformacionConfig;
import com.monicash.monicash.bean.ZiLiPageUserDataBean;
import com.monicash.monicash.information.CapitalInforActivity;
import com.monicash.monicash.information.modle.KycModle;
import com.monicash.monicash.util.T;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class FamilialInformationFragment extends BaseFragment {

    TextView tv_submit_to;

    EditText et_city;
    EditText et_commune;

    EditText et_address;

    EditText et_region;

    EditText et_marital_status;






    View view = null;

    public KycModle kycModle = null;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_familial_information, container, false);
        kycModle = KycModle.Companion.getInstant();
        initView();
        initData();
        return view;
    }

    @Override
    public void initView() {
        tv_submit_to = view.findViewById(R.id.tv_submit_to);
        et_city = view.findViewById(R.id.et_city);
        et_commune = view.findViewById(R.id.et_commune);
        et_address = view.findViewById(R.id.et_address);
        et_region = view.findViewById(R.id.et_region);
        et_marital_status = view.findViewById(R.id.et_marital_status);

        tv_submit_to.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CapitalInforActivity capitalInforActivity = (CapitalInforActivity) getActivity();
                if(capitalInforActivity!=null){
                    capitalInforActivity.onTabSelected(capitalInforActivity.TAG_INDEX_THREE);
                }
            }
        });

    }

    @Override
    public void initData() {
        getZiLiPageUserFullInfo();
        getZiLiPageUserInfoConfig();

    }

    private void getZiLiPageUserInfoConfig() {
        Map<String, String> params = new HashMap<String, String>();
        String data = kycModle.getZiLiPageUserInfoConfig(params);
        try {
            JSONObject jsonObject = new JSONObject(data);
            AppKycPersonalInformacionConfig applicationUserInfoConfigBean = new AppKycPersonalInformacionConfig();
            applicationUserInfoConfigBean.initConfigData(jsonObject.optJSONObject("data"));
            AppKycPersonalInformacionConfig.UserInfoConfig userInfoConfig = applicationUserInfoConfigBean.getUserInfoConfig();
            if (applicationUserInfoConfigBean.getUserInfoConfig() != null) {
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getZiLiPageUserFullInfo() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("custId", ZiLiPageUserDataBean.getInstance().getCustomerId());
        String data = kycModle.getZiLiPageUserFullInfo(params);

        try {
            JSONObject jsonObject = new JSONObject(data);
            if (kycModle.isSuccess(jsonObject)) {
                showHistoryData(jsonObject.optJSONObject("data"));
            } else {
                T.showShort(getActivity(), kycModle.getStringToJson(jsonObject, "msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showHistoryData(JSONObject jsonObject) {
        et_city.setText(kycModle.getStringToJson(jsonObject, "city"));
        et_region.setText(kycModle.getStringToJson(jsonObject, "province"));
        et_address.setText(kycModle.getStringToJson(jsonObject, "currentAddress"));

        String marital_status = kycModle.getStringToJson(jsonObject, "maritalStatus");
        String[] maritalStatusValues = new String[]{"Soltero", "Casado", "Divorciado", "Viudo"};
        String maritalStatusValue = "";
//        "S", "M", "D", "W"
        switch (marital_status){
            case "S":
                maritalStatusValue = maritalStatusValues[0];
                break;
            case "M":
                maritalStatusValue = maritalStatusValues[1];
                break;
            case "D":
                maritalStatusValue = maritalStatusValues[2];
                break;
            case "W":
                maritalStatusValue = maritalStatusValues[3];
                break;
        }
        et_marital_status.setText(maritalStatusValue);

        String[] relationValues = new String[]{"padres", "cónyuge", "hijos", "hermanos", "amigo", "colega"};
        JSONArray contactInfoJa = jsonObject.optJSONArray("contactInfo");

    }

}
